/*
 * note.c
 *
 *  Created on: Feb 27, 2013
 *      Author: Vincent
 */
#include "support_common.h" /* include peripheral declarations and more */
#include <stdio.h>
#include "note.h"


Note create_note(Pitch p, Duration d)
{
        Note temp;
        temp.p = p;
        temp.d = d;
        return temp;
}

void set_pitch (Note newnote)
{
        switch (newnote.p){
                case Pitch_E4: //329.63 Hz, 3.034 ms
                                        MCF_PWM_PWMPRCLK = 0x0;
                                        MCF_PWM_PWMSCLB = 0xEE;
                                        MCF_PWM_PWMPER7 = 0xFF;
                                        MCF_PWM_PWMDTY7 = 0xCC;
                                        break;
                case Pitch_G4: // 392Hz, 2.551 ms
                                        MCF_PWM_PWMPRCLK = 0x00;
                                        MCF_PWM_PWMSCLB = 0xC8;
                                        MCF_PWM_PWMPER7 = 0xFF;
                                        MCF_PWM_PWMDTY7 = 0xCC;
                                        break;
                case Pitch_C4:
                                        MCF_PWM_PWMPRCLK = 0x01;
                                        MCF_PWM_PWMSCLB = 0x96;
                                        MCF_PWM_PWMPER7 = 0xFF;
                                        MCF_PWM_PWMDTY7 = 0xCC;
                                        break;
                case Pitch_F4:
                                        MCF_PWM_PWMPRCLK = 0x00;
                                        MCF_PWM_PWMSCLB = 0xE1;
                                        MCF_PWM_PWMPER7 = 0xE6;
                                        MCF_PWM_PWMDTY7 = 0xB8;
                                        break;
                case Pitch_D4:
                                        MCF_PWM_PWMPRCLK = 0x00;
                                        MCF_PWM_PWMSCLB = 0xF9;
                                        MCF_PWM_PWMPER7 = 0xE6;
                                        MCF_PWM_PWMDTY7 = 0xB8;
                                        break;
                                        
                case Pitch_00:
                						MCF_PWM_PWMPER7 = 0x01;
                	                    MCF_PWM_PWMDTY7 = 0x01;
                	                    break;
                default:
                        break;
                       
        }
}

void set_duration (Note newnote)
{
        switch (newnote.d){
                case Duration_Quarter: //Count = 1221
                						MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x04C5);
                                        break;
                case Duration_Half: //Count = 2441
                						MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x0989);
                                        break;
                case Duration_Whole: // Count = 4883
                                        MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x1313);
                                        break;
                default:
                        break;
                       
        }
}

void set_delay (int bpm)
{
	switch (bpm)
	{
		case 60 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x1313); //4663
			break;
		case 70 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x1059); //4185
			break;
		case 80 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x0E4E); //3662
			break;
		case 90 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x0CB7); //3255
			break;
		case 100 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x0B72); //2930
			break;
		case 110 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x0A67); //2663
			break;
		case 120 :
			MCF_PIT0_PMR  |= MCF_PIT_PMR_PM(0x0989); //2441
			break;
		default:
			break;
	}
			
        
}

